-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[clang-reorder-fields] Check for flexible array member #160262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexander-shaposhnikov
merged 1 commit into
llvm:main
from
vvuksanovic:reorder-fields-flexible-array-member
Sep 29, 2025
Merged
[clang-reorder-fields] Check for flexible array member #160262
alexander-shaposhnikov
merged 1 commit into
llvm:main
from
vvuksanovic:reorder-fields-flexible-array-member
Sep 29, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A flexible array member must remain the last field in the struct.
|
@llvm/pr-subscribers-clang-tools-extra Author: Vladimir Vuksanovic (vvuksanovic) ChangesA flexible array member must remain the last field in the struct. Full diff: https://github.com/llvm/llvm-project/pull/160262.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp b/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
index affa276a0c550..5770bf767bc3c 100644
--- a/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
+++ b/clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
@@ -164,6 +164,22 @@ getNewFieldsOrder(const RecordDecl *Definition,
return NewFieldsOrder;
}
+static bool isOrderValid(const RecordDecl *RD, ArrayRef<unsigned> FieldOrder) {
+ if (FieldOrder.empty())
+ return false;
+
+ // If there is a flexible array member in the struct, it must remain the last
+ // field.
+ if (RD->hasFlexibleArrayMember() &&
+ FieldOrder.back() != FieldOrder.size() - 1) {
+ llvm::errs()
+ << "Flexible array member must remain the last field in the struct\n";
+ return false;
+ }
+
+ return true;
+}
+
struct ReorderedStruct {
public:
ReorderedStruct(const RecordDecl *Decl, ArrayRef<unsigned> NewFieldsOrder)
@@ -662,7 +678,7 @@ class ReorderingConsumer : public ASTConsumer {
return;
SmallVector<unsigned, 4> NewFieldsOrder =
getNewFieldsOrder(RD, DesiredFieldsOrder);
- if (NewFieldsOrder.empty())
+ if (!isOrderValid(RD, NewFieldsOrder))
return;
ReorderedStruct RS{RD, NewFieldsOrder};
@@ -699,7 +715,7 @@ class ReorderingConsumer : public ASTConsumer {
std::unique_ptr<ASTConsumer> ReorderFieldsAction::newASTConsumer() {
return std::make_unique<ReorderingConsumer>(RecordName, DesiredFieldsOrder,
- Replacements);
+ Replacements);
}
} // namespace reorder_fields
diff --git a/clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c b/clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c
new file mode 100644
index 0000000000000..ef64350fd08e6
--- /dev/null
+++ b/clang-tools-extra/test/clang-reorder-fields/FlexibleArrayMember.c
@@ -0,0 +1,10 @@
+// RUN: clang-reorder-fields -record-name Foo -fields-order z,y,x %s -- 2>&1 | FileCheck --check-prefix=CHECK-BAD %s
+// RUN: clang-reorder-fields -record-name Foo -fields-order y,x,z %s -- | FileCheck --check-prefix=CHECK-GOOD %s
+
+// CHECK-BAD: {{^Flexible array member must remain the last field in the struct}}
+
+struct Foo {
+ int x; // CHECK-GOOD: {{^ int y;}}
+ int y; // CHECK-GOOD-NEXT: {{^ int x;}}
+ int z[]; // CHECK-GOOD-NEXT: {{^ int z\[\];}}
+};
|
alexander-shaposhnikov
approved these changes
Sep 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lg
|
Thanks, can you merge? |
mahesh-attarde
pushed a commit
to mahesh-attarde/llvm-project
that referenced
this pull request
Oct 3, 2025
A flexible array member must remain the last field in the struct.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A flexible array member must remain the last field in the struct.